<?php
//liveSearch.php
//returns a list of search results as a JSON formatted string
	require_once("includes/overhead.php");
	$courseParts = explode(" ",mysql_real_escape_string($_REQUEST['generalSearch']));
	$d = new Department();
	$output="";
	$results=null;
	if ($d->retrieveFromCode(mysql_real_escape_string($courseParts[0]))) { //Department found, so assume they are searching for a course.
		$getCoursesQuery = mysql_query("select Course.ID as CourseID, Course.Number as CourseNumber, (select round(avg(GPA),2) from Section as S where S.CourseID=Course.ID) as GPA from Course where DepartmentID=".$d->getID().(array_key_exists(1,$courseParts) ? " and Course.Number like '".mysql_real_escape_string($courseParts[1])."%'" : (isset($_REQUEST['noLimit']) ? "" : " limit 15")));
		while ($row = mysql_fetch_assoc($getCoursesQuery)) {
			$row['DepartmentCode']=$d->getCode();
			$results[]=$row;
			}
			$output=json_encode($results);
	} else { 
		//assume it's a professor
		$getProfessorsQuery=mysql_query("select Professor.ID as ProfessorID, Professor.LastName as LastName, Professor.FirstName as FirstName, (select round(avg(GPA),2) from Section as S where S.ProfessorID=Professor.ID) as GPA from Professor where LastName like '".mysql_real_escape_string($_REQUEST['generalSearch'])."%' order by LastName, FirstName ".(isset($_REQUEST['noLimit']) ? "" : " limit 10"));
		while ($row = mysql_fetch_assoc($getProfessorsQuery)) {
			$results[]=$row;
		}

	}
	$output=json_encode($results);
	if ($output != "") { print $output; }
 ?>
